package city.spring.modules.reception.controller;

import city.spring.modules.oldman.service.OldmanInfoService;
import city.spring.modules.reception.entity.AdvisoryEntity;
import city.spring.modules.reception.service.AdvisoryService;
import city.spring.modules.reception.service.ReservationService;
import city.spring.modules.reception.vo.AdvisoryInfoVO;
import city.spring.modules.reception.vo.AdvisoryListVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：咨询记录
 *
 * @author 侯坤林
 * @date 2020-06-23 10:56:54
 */
@RestController
@RequestMapping("/advisory")
public class AdvisoryController extends ApiController {
    private final AdvisoryService advisoryService;
    private final OldmanInfoService oldmanInfoService;
    private final ReservationService reservationService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<AdvisoryEntity, ?>> entityOrderFields;

    public AdvisoryController(AdvisoryService advisoryService, OldmanInfoService oldmanInfoService, ReservationService reservationService) {
        this.advisoryService = advisoryService;
        this.oldmanInfoService = oldmanInfoService;
        this.reservationService = reservationService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(AdvisoryEntity::getId);
        entityOrderFields.add(AdvisoryEntity::getOldmanId);
        entityOrderFields.add(AdvisoryEntity::getReceptionUserId);
        entityOrderFields.add(AdvisoryEntity::getName);
        entityOrderFields.add(AdvisoryEntity::getTelephone);
        entityOrderFields.add(AdvisoryEntity::getRelationshipOldman);
        entityOrderFields.add(AdvisoryEntity::getDatetime);
        entityOrderFields.add(AdvisoryEntity::getWay);
        entityOrderFields.add(AdvisoryEntity::getMediaChannel);
        entityOrderFields.add(AdvisoryEntity::getType);
        entityOrderFields.add(AdvisoryEntity::getContent);
        entityOrderFields.add(AdvisoryEntity::getReviewUserId);
        entityOrderFields.add(AdvisoryEntity::getReviewDatetime);
        entityOrderFields.add(AdvisoryEntity::getReviewContent);
        entityOrderFields.add(AdvisoryEntity::getGmtCreate);
        entityOrderFields.add(AdvisoryEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>咨询记录</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, AdvisoryEntity entity) {
        LambdaQueryChainWrapper<AdvisoryEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>咨询记录</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, AdvisoryListVO entity) {
        QueryWrapper<AdvisoryListVO> queryWrapper = buildQuery(entity);
        return success(advisoryService.queryPage(MyBatisUtils.toPage(pageable, true), queryWrapper));
    }

    /**
     * 获取一个 <strong>咨询记录</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        AdvisoryEntity advisoryEntity = advisoryService.getById(id);
        if (advisoryEntity == null) {
            throw new RuntimeException("找不到该咨询记录");
        }
        AdvisoryInfoVO vo = new AdvisoryInfoVO();
        vo.setAdvisory(advisoryEntity);
        vo.setOldmanInfo(oldmanInfoService.getById(advisoryEntity.getOldmanId()));
        vo.setReservation(reservationService.getById(advisoryEntity.getReservationId()));
        return success(vo);
    }

    /**
     * 添加一个 <strong>咨询记录</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody AdvisoryInfoVO entity) {
        advisoryService.saveAdvisory(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getAdvisory().getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>咨询记录</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody AdvisoryInfoVO entity) {
        entity.getAdvisory().setId(id);
        advisoryService.updateAdvisory(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>咨询记录</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        advisoryService.deleteAdvisory(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>咨询记录</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        advisoryService.deleteAdvisory(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<AdvisoryEntity> buildLambdaQuery(AdvisoryEntity entity) {
        LambdaQueryChainWrapper<AdvisoryEntity> lambdaQuery = advisoryService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), AdvisoryEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), AdvisoryEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getReceptionUserId()), AdvisoryEntity::getReceptionUserId, entity
                .getReceptionUserId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getName()), AdvisoryEntity::getName, entity.getName());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTelephone()), AdvisoryEntity::getTelephone, entity.getTelephone());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRelationshipOldman()), AdvisoryEntity::getRelationshipOldman, entity
                .getRelationshipOldman());
        lambdaQuery.eq(entity.getDatetime() != null, AdvisoryEntity::getDatetime, entity.getDatetime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getWay()), AdvisoryEntity::getWay, entity.getWay());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getMediaChannel()), AdvisoryEntity::getMediaChannel, entity.getMediaChannel());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getType()), AdvisoryEntity::getType, entity.getType());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getContent()), AdvisoryEntity::getContent, entity.getContent());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getReviewUserId()), AdvisoryEntity::getReviewUserId, entity.getReviewUserId());
        lambdaQuery.eq(entity.getReviewDatetime() != null, AdvisoryEntity::getReviewDatetime, entity.getReviewDatetime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getReviewContent()), AdvisoryEntity::getReviewContent, entity.getReviewContent());
        lambdaQuery.eq(entity.getGmtCreate() != null, AdvisoryEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, AdvisoryEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<AdvisoryListVO> buildQuery(AdvisoryListVO entity) {
        QueryWrapper<AdvisoryListVO> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanId()), "t1.oldman_id", entity.getOldmanId());
        wrapper.like(StringUtils.isNotBlank(entity.getReceptionUserId()), "t1.reception_user_id", entity.getReceptionUserId());
        wrapper.like(StringUtils.isNotBlank(entity.getName()), "t1.name", entity.getName());
        wrapper.like(StringUtils.isNotBlank(entity.getTelephone()), "t1.telephone", entity.getTelephone());
        wrapper.like(StringUtils.isNotBlank(entity.getRelationshipOldman()), "t1.relationship_oldman", entity.getRelationshipOldman());
        wrapper.eq(entity.getDatetime() != null, "t1.datetime", entity.getDatetime());
        wrapper.like(StringUtils.isNotBlank(entity.getWay()), "t1.way", entity.getWay());
        wrapper.like(StringUtils.isNotBlank(entity.getMediaChannel()), "t1.media_channel", entity.getMediaChannel());
        wrapper.like(StringUtils.isNotBlank(entity.getType()), "t1.type", entity.getType());
        wrapper.like(StringUtils.isNotBlank(entity.getContent()), "t1.content", entity.getContent());
        wrapper.like(StringUtils.isNotBlank(entity.getReviewUserId()), "t1.review_user_id", entity.getReviewUserId());
        wrapper.like(StringUtils.isNotBlank(entity.getReviewUsername()), "t4.name", entity.getReviewUsername());
        wrapper.eq(entity.getReviewDatetime() != null, "t1.review_datetime", entity.getReviewDatetime());
        wrapper.like(StringUtils.isNotBlank(entity.getReviewContent()), "t1.review_content", entity.getReviewContent());
        wrapper.eq(entity.getGmtCreate() != null, "t1.gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "t1.gmt_modified", entity.getGmtModified());

        // 查询老人信息
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanName()), "t2.oldman_name", entity.getOldmanName());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanFamilyAddress()), "t2.family_address", entity.getOldmanFamilyAddress());
        return wrapper;
    }

}
